package com.cogent.validator.impl;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.validator.IValidator;
import com.cogent.validator.domain.ValidatorDomain;

public class RegularValidator implements IValidator{
	private static HashMap REGULAR_RULE_MAP = new HashMap();
	private static HashMap SERVER_REGULAR_RULE_MAP = new HashMap();
	private static HashMap NOTE_MESSAGE_MAP = new HashMap();
	static {
		REGULAR_RULE_MAP.put("number", "^[0-9]*$");
		REGULAR_RULE_MAP.put("phone", "^[\\\\(\\\\)\\\\-0-9]+$");
		REGULAR_RULE_MAP.put("number10_2", "^([0-9]\\\\d{0,9})$|^([0-9]\\\\d{0,9}\\\\.{1}\\\\d{0,2})$");
		REGULAR_RULE_MAP.put("date", "^((((1[6-9]|[2-9]\\\\d)\\\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\\\d|3[01]))|(((1[6-9]|[2-9]\\\\d)\\\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\\\d|30))|(((1[6-9]|[2-9]\\\\d)\\\\d{2})-0?2-(0?[1-9]|1\\\\d|2[0-8]))|(((1[6-9]|[2-9]\\\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))$");
	}
	static {
		SERVER_REGULAR_RULE_MAP.put("number", "^[0-9]*$");
		SERVER_REGULAR_RULE_MAP.put("number10_2", "^([0-9]\\d{0,9})|([0-9]\\d{0,9}\\.{1}\\d{0,2})$");
		SERVER_REGULAR_RULE_MAP.put("phone", "^[\\(\\)\\-0-9]+$");
		SERVER_REGULAR_RULE_MAP.put("date", "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))$");
	}
	static {
		NOTE_MESSAGE_MAP.put("number", " 格式不正确!只能输入整数,形如2001");
		NOTE_MESSAGE_MAP.put("phone", " 格式不正确!请输入有效的电话号码.");
		NOTE_MESSAGE_MAP.put("date", " 格式不正确!形如2001-12-11");
		NOTE_MESSAGE_MAP.put("number10_2", " 格式不正确!小数点前10位数字,小数点后2位数字;形如102或102.23");
	}
	
	public String getScript(ValidatorDomain domain) {
		StringBuffer str = new StringBuffer();
		HashMap curMap = domain.getKeypairmap();
		String temp = MapUtil.getStringValue(NOTE_MESSAGE_MAP, MapUtil.getStringValue(curMap, "regulartype"));
		if(StringUtil.isEmpty(temp))
			temp = domain.getKey() + " 格式不正确!";
		else
			temp = domain.getKey() + temp;
		if(!StringUtil.isEmpty(domain.getNotemsg()))
			temp = domain.getNotemsg();
		str.append("regularValidator(\"");
		if(!StringUtil.isEmpty(domain.getPrefix()))
			str.append(domain.getPrefix()).append("\" + curnum + \"]" + domain.getInfix() + "\" + \"");
		str.append(domain.getProperty());
		str.append("\",\"").append(temp).append("\",\"")
			.append(MapUtil.getStringValue(REGULAR_RULE_MAP, MapUtil.getStringValue(curMap, "regulartype")))
			.append("\",errmsg").append(");");
		
		return str.toString();	 
	}
	
	public String validator(HashMap fromMap, ValidatorDomain domain) {
		String ret = "";
		HashMap curMap = domain.getKeypairmap();
		String value = MapUtil.getStringValue(fromMap, domain.getProperty());
		if(!StringUtil.isEmpty(value)) {
			String temp = MapUtil.getStringValue(NOTE_MESSAGE_MAP, MapUtil.getStringValue(curMap, "regulartype"));
			if(StringUtil.isEmpty(temp))
				temp = domain.getKey() + " 格式不正确!";
			if(!StringUtil.isEmpty(domain.getNotemsg()))
				temp = domain.getNotemsg();
			String regular = MapUtil.getStringValue(SERVER_REGULAR_RULE_MAP, MapUtil.getStringValue(curMap, "regulartype"));
			Pattern pattern = Pattern.compile(regular);
			Matcher matcher = pattern.matcher(value); 
			if(!matcher.matches())
				ret = temp;
		}

		return ret;
	}

}
